package com.hzit.ms.service.impl;

import com.hzit.ms.controller.GoodsController;
import com.hzit.ms.mapper.SkGoodsInfoMapper;
import com.hzit.ms.mapper.SkOrderInfoMapper;
import com.hzit.ms.model.SkOrderInfo;
import com.hzit.ms.model.SkillGoodsInfo;
import com.hzit.ms.resp.RespVo;
import com.hzit.ms.service.ISkillService;
import com.hzit.ms.vo.CreateOrderReqData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 秒杀接口实现
 */
@Service
public class SkillServiceImpl implements ISkillService, InitializingBean {

    @Autowired
    private ValueOperations<String,Object> valueOperations;

    @Autowired
    private SkGoodsInfoMapper skGoodsInfoMapper;

    @Autowired
    private SkOrderInfoMapper skOrderInfoMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    private Logger logger = LoggerFactory.getLogger(SkillServiceImpl.class);


    /**
     * 秒杀
     * @param userId
     * @param goodsId
     */
    @Override
    public RespVo getSkGoods(String userId, String goodsId) {

        RespVo respVo = new RespVo();
        //1.查询库存是否足够。 预库存
        String stockRedisKey = "sk:goods:stock:"+goodsId;
        long stockNum = valueOperations.decrement(stockRedisKey,1);

        if (stockNum <0){
            respVo.setCode(-1);
            respVo.setMsg("商品已售完");
            logger.info("商品已售完");
            return respVo;
        }

        //2.判断用户是否已经生成过订单
        List<SkOrderInfo> skOrderInfoList = skOrderInfoMapper.queryByUserIdAndGoodsId(userId,goodsId);
        if (!CollectionUtils.isEmpty(skOrderInfoList)){
            respVo.setCode(-1);
            respVo.setMsg("已有秒杀订单");
            logger.info("已有秒杀订单");
            return respVo;
        }

        logger.info("------------------------------");
        CreateOrderReqData orderReqData = new CreateOrderReqData();
        orderReqData.setGoodsId(goodsId);
        orderReqData.setUserId(userId);
        rabbitTemplate.convertAndSend("ms.exchange.topic","create.order",orderReqData);

        respVo.setCode(0);
        respVo.setMsg("正在抢购中");

        return respVo ;

    }

    /**
     * 跟随服务器一起启动
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {

        //获取秒杀商品的数量
        List<SkillGoodsInfo> skillGoodsInfos = skGoodsInfoMapper.querySkillGoods();

        if(CollectionUtils.isEmpty(skillGoodsInfos)){
            logger.info("##########没有找到可用秒杀商品########");
            return;
        }

        String stockRedisKey = "sk:goods:stock:";
        for (SkillGoodsInfo goodsInfo :skillGoodsInfos){
            valueOperations.set(stockRedisKey + goodsInfo.getGoodsId(),goodsInfo.getStockCount());
        }

        logger.info("##########秒杀商品初始化完成########");

    }
}
